package com.dp.service.impl3;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.service.impl3.BaseImpl;
import com.dp.util.KeyGenerator;
import com.dp.vo.TSubmenus;

@Service
@Transactional
public class SubmenusServiceImpl3 extends BaseImpl {

	/**
	 * 查询
	 * 
	 * @return
	 */
	public Long findSubmenusCount(TSubmenus submenus,JsonPage jpage) {
			StringBuffer hql = new StringBuffer("SELECT count(a) FROM TSubmenus a where 1=1 ");
			if (submenus != null) {
				if (submenus.getMainmenuId() != null
						&& !"".equals(submenus.getMainmenuId())) {
					hql.append(" and a.mainmenuId like '" + submenus.getMainmenuId() + "'");
				}
				if (submenus.getIsUse() != null
						&& !"".equals(submenus.getIsUse())) {
					hql.append(" and a.isUse like '" + submenus.getIsUse() + "'");
				}
			}
			if(jpage!=null){
				if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
					hql.append(" order by a."+jpage.getSidx());
					hql.append(" "+jpage.getSord());
				}
			}
			Object obj = getSingleResult(hql.toString());
			if (obj == null) {
				return Long.valueOf(0 + "");
			} else {
				return (Long) obj;
			}
	}

	/**
	 * 查询,分页
	 * 
	 * @param clientua
	 * @return
	 */
	public List<TSubmenus> findSubmenus(TSubmenus submenus,JsonPage jpage, int firstrow, int maxrow) {
		StringBuffer hql = new StringBuffer("SELECT a FROM TSubmenus a where 1=1 ");
		if (submenus != null) {
			if (submenus.getMainmenuId() != null
					&& !"".equals(submenus.getMainmenuId())) {
				hql.append(" and a.mainmenuId like '" + submenus.getMainmenuId() + "'");
			}
			if (submenus.getIsUse() != null
					&& !"".equals(submenus.getIsUse())) {
				hql.append(" and a.isUse like '" + submenus.getIsUse() + "'");
			}
		}
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				hql.append(" order by a."+jpage.getSidx());
				hql.append(" "+jpage.getSord());
			}
		}
		return getQueryList(hql.toString(), firstrow, maxrow);
	}

	/*public TSubmenus findAdById(String submenuid) {
		String hql = "SELECT a FROM TSubmenus a where 1=1 ";
		if (submenuid != null && !"".equals(submenuid)) {
			hql += " and a.submenuId = '" + submenuid + "'";
		}
		Object obj = getSingleResult(hql);
		if (obj == null) {
			return null;
		} else {
			return (TSubmenus) obj;
		}
	}*/

	public int update(TSubmenus submenus) {
		String hql = "UPDATE TSubmenus SET submenuName='" + submenus.getSubmenuName()
		+ "',ctrlUrl='" + submenus.getCtrlUrl()
		+ "',target='" + submenus.getTarget()
		+ "',isUse='" + submenus.getIsUse()
		+ "',orderid=" + submenus.getOrderid()
				+ " where submenuId='" + submenus.getSubmenuId()+"'";
		return super.updateHql(hql);
	}

	public Object save(TSubmenus submenus) {
		String submenuId = com.dp.util.BaseBean.ObjectToString(KeyGenerator.getInstance().getNextKeyValue("t_submenus"));	//获取主键（非自增）
		submenus.setSubmenuId(submenuId);
		return super.save(submenus);
	}

	/*public int checkSubmenusName(String submenuName, String old_submenuName) {
		String hql = "SELECT a FROM TSubmenus a where 1=1 ";
		if (submenuName != null && !"".equals(submenuName)) {
			hql += " and a.submenuName = '" + submenuName + "'";
		}
		if (old_submenuName != null && !"".equals(old_submenuName)) {
			hql += " and a.submenuName != '" + old_submenuName + "'";
		}
		Object obj = getSingleResult(hql);
		if (obj == null) {
			return 0;
		} else {
			return 1;
		}
	}*/

	public TSubmenus checkSubmenus_submenuName(String submenuName) {
		String hql = "SELECT a FROM TSubmenus a where 1=1 ";
		if (submenuName != null && !"".equals(submenuName)) {
			hql += " and a.submenuName = '" + submenuName + "'";
		}
		Object obj = getSingleResult(hql);
		if (obj == null) {
			return null;
		} else {
			return (TSubmenus) obj;
		}
	}

	/**
	 * Delete NewsBlacklist Entity
	 * 
	 * @param urlB
	 * @return boolean b
	 */
	public boolean deleteSubmenusById(String submenuId) {
		String jpql = "DELETE TSubmenus n WHERE n.submenuId='" + submenuId + "'";
		if (super.updateHql(jpql) > 0)
			return true;
		else
			return false;
	}

}
